<?php

class Tanulmanyi extends Page {

    protected $template = 'tanulmanyi.tpl';
    protected $customTemplate = true;
    const STUDYC_TABLE = 'study_contract';
    const DISC_TABLE = 'discipline';
    const TEACHER_TABLE = 'teacher';

    public function __construct() {
        parent::__construct();
        $this->load();
         if (isset($_POST['save'])) {
            $this->save();
        }
        if (isset($_POST['saveToDoc'])) {
            $this->saveToPdf();
        }
    }

    private function load() {
        $id = App::getActiveUser()->getId();

        $userinfo = Dal::fetchAll(" SELECT UI.Name AS UIName, UI.Semester  FROM " .
                        User::USER_TABLE . " U INNER JOIN " .
                        User::INFO_TABLE . " UI ON UI.UserId = U.Id " .
                        " WHERE U.Id = {$id} ");
        
         $disciplines1 = Dal::fetchAll(" SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester FROM " .
                        self::DISC_TABLE . " D INNER JOIN " .
                        self::TEACHER_TABLE . " T ON T.Id = D.TeacherId AND D.Semester = 1");
         $disciplines2 = Dal::fetchAll(" SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester FROM " .
                        self::DISC_TABLE . " D INNER JOIN " .
                        self::TEACHER_TABLE . " T ON T.Id = D.TeacherId AND D.Semester = 2");
         $disciplines3 = Dal::fetchAll(" SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester FROM " .
                        self::DISC_TABLE . " D INNER JOIN " .
                        self::TEACHER_TABLE . " T ON T.Id = D.TeacherId AND D.Semester = 3");
         $disciplines4 = Dal::fetchAll(" SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester FROM " .
                        self::DISC_TABLE . " D INNER JOIN " .
                        self::TEACHER_TABLE . " T ON T.Id = D.TeacherId AND D.Semester = 4");
         $disciplines5 = Dal::fetchAll(" SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester FROM " .
                        self::DISC_TABLE . " D INNER JOIN " .
                        self::TEACHER_TABLE . " T ON T.Id = D.TeacherId AND D.Semester = 5");
         $disciplines6 = Dal::fetchAll(" SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester FROM " .
                        self::DISC_TABLE . " D INNER JOIN " .
                        self::TEACHER_TABLE . " T ON T.Id = D.TeacherId AND D.Semester = 6");
         $disciplines7 = Dal::fetchAll(" SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester FROM " .
                        self::DISC_TABLE . " D INNER JOIN " .
                        self::TEACHER_TABLE . " T ON T.Id = D.TeacherId AND D.Semester = 7");
         $disciplines8 = Dal::fetchAll(" SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester FROM " .
                        self::DISC_TABLE . " D INNER JOIN " .
                        self::TEACHER_TABLE . " T ON T.Id = D.TeacherId AND D.Semester = 8");

        $sql = "SELECT D.Id, D.Name AS DName, D.Credit, D.CourseHours, D.Type, T.Name AS TName, D.Semester  FROM " .
                User::USER_TABLE . " U INNER JOIN " .
                User::INFO_TABLE . " UI ON UI.UserId = U.Id INNER JOIN " .
                self::STUDYC_TABLE . " SC ON SC.UserId = U.Id INNER JOIN " .
                self::DISC_TABLE . " D ON SC.DiscId = D.Id INNER JOIN " .
                self::TEACHER_TABLE . " T ON T.Id = D.TeacherId " .
                " WHERE U.Id = {$id} ";


        $studyc = Dal::fetchAll($sql);

        $this->smarty->assign('userinfo', $userinfo);
        $this->smarty->assign('disciplines1', $disciplines1);
        $this->smarty->assign('disciplines2', $disciplines2);
        $this->smarty->assign('disciplines3', $disciplines3);
        $this->smarty->assign('disciplines4', $disciplines4);
        $this->smarty->assign('disciplines5', $disciplines5);
        $this->smarty->assign('disciplines6', $disciplines6);
        $this->smarty->assign('disciplines7', $disciplines7);
        $this->smarty->assign('disciplines8', $disciplines8);
        $this->smarty->assign('studyc', $studyc);

        $sql = "Select Name from teacher";
        $tanarnev = DAL::fetchAll($sql);
        sort($tanarnev);
        $this->smarty->assign('tanarnev', $tanarnev);
    }

    private function save() {
        
        $chbox = $_POST['selected'];
        foreach ($chbox as $discid) {
        }
        echo $chbox;
            die();
    }

    private function add() {
        /*
          $tnev = $_POST['tnev'];
          $credit = $_POST['credit'];
          $hour = $_POST['hour'];
          $type = $_POST['type'];
          $semester =$_POST['semester'];

          echo $tnev;
          echo $credit; */
        die();
    }

    private function saveToPdf() {
        require_once("dompdf/dompdf_config.inc.php");
        spl_autoload_register('DOMPDF_autoload');

        $html =
                '<html><body>' .
                '<p>Put your html here, or generate it with your favourite ' .
                'templating system.</p>' .
                '</body></html>';

        $dompdf = new DOMPDF();
        $dompdf->load_html($this->smarty->fetch("pdftanulmanyi.tpl"));
        $dompdf->render();
//        $dompdf->output();
        $dompdf->stream("sample.pdf");
    }

}
?>